package com.fau.hm.action.index;

import java.io.File;
import java.util.HashMap;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.context.annotation.Scope;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Controller;

import com.fau.hm.action.PublicAction;
import com.fau.hm.entity.MedicalAdmin;
import com.fau.hm.entity.MedicalArchivalInfo;
import com.fau.hm.entity.MedicalKeyValue;
import com.fau.hm.util.SystemConfig;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ModelDriven;

/**
 * @desc 档案管理action
 * @author dangjh
 * @datetime 2014年12月15日,上午2:57:18
 */
@Controller
@Scope("prototype")
@ParentPackage("default")
@Namespace("/")
public class ArchivalAction extends PublicAction implements ModelDriven<MedicalArchivalInfo> {

	private static final long serialVersionUID = -7597865667110059193L;

	private MedicalArchivalInfo info = new MedicalArchivalInfo();
	
	/**
	 * @desc 实例化档案管理界面
	 * @return
	 * @author dangjh
	 * @datetime 2014年10月28日,下午9:08:13
	 */
	@Action(value = "init_archival_list", results = {@Result(location = "init_archival_list.jsp")})
	public String initArchival(){
		return SUCCESS;
	}
	
	/**
	 * @desc 加载档案信息列表数据
	 * @return
	 * @author dangjh
	 * @datetime 2014年10月28日,下午9:10:39
	 */
	@Action(value = "medical_archival_view")
	public String archivalView(){
		this.map = ActionContext.getContext().getParameters();
		MedicalAdmin admin = (MedicalAdmin) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
		this.pageBean = this.baseAllService.getArchivalService().findArchivalList(map, info, admin);
		this.map.clear();
		map.put("total", this.pageBean.getTotalRow());
		map.put("data", this.pageBean.getResult());
		HttpServletResponse response = ServletActionContext.getResponse();
		formatJSON(response, this.map);
		return null;
	}
	
	/**
	 * @desc 实例化档案信息添加界面
	 * @return
	 * @author dangjh
	 * @datetime 2014年10月28日,下午9:11:40
	 */
	@Action(value = "medical_archival_add", results = {@Result(location = "medical_archival_add.jsp")})
	public String archivalAdd(){
		return SUCCESS;
	}
	
	/**
	 * @desc 保存档案信息
	 * @return
	 * @author dangjh
	 * @datetime 2014年10月28日,下午9:13:13
	 */
	@Action(value = "medical_archival_save")
	public String archivalSave(){
		HttpServletResponse response = ServletActionContext.getResponse();
		map = new HashMap<String, Object>();
		boolean flag = this.baseAllService.getArchivalService().save(info);
		if(flag){
			map.put("success", true);
		}else{
			map.put("success", false);
		}
		formatJSON(response, map);
		return null;
	}
	
	/**
	 * @desc 实例化档案信息编辑界面
	 * @return
	 * @author dangjh
	 * @datetime 2014年10月28日,下午9:14:46
	 */
	@Action(value = "medical_archival_edit", results = {@Result(location = "medical_archival_edit.jsp")})
	public String archivalEdit(){
		return SUCCESS;
	}
	
	/**
	 * @desc 保存修改的档案信息
	 * @return
	 * @author dangjh
	 * @datetime 2014年10月28日,下午9:15:48
	 */
	@Action(value = "archival_save_edit")
	public String archivalSaveEdit(){
		HttpServletResponse response = ServletActionContext.getResponse();
		map = new HashMap<String, Object>();
		if(StringUtils.isNotBlank(info.getId())){
			boolean flag = this.baseAllService.getArchivalService().update(info);
			if(flag){
				map.put("success", true);
			}else{
				map.put("success", false);
			}
		}else{
			map.put("success", false);
		}
		formatJSON(response, map);
		return null;
	}
	
	/**
	 * @desc 上传档案相关文件
	 * @return
	 * @author dangjh
	 * @datetime 2014年10月28日,下午9:17:19
	 */
	@Action(value = "archival_file_upload")
	public String uploadFileAndSave(){
		HttpServletRequest request = ServletActionContext.getRequest();
		HttpServletResponse response = ServletActionContext.getResponse();
		map = new HashMap<String, Object>();
		MedicalAdmin admin = (MedicalAdmin) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
		MedicalKeyValue keyVal1 = this.baseAllService.getDictionaryService().findKeyValByKey(admin, SystemConfig.HM_SYSTEM_TYPE, SystemConfig.UPLOAD_ARCHIVAL_KEY);
		MedicalKeyValue keyVal2 = this.baseAllService.getDictionaryService().findKeyValByKey(admin, SystemConfig.HM_SYSTEM_TYPE, SystemConfig.UPLOAD_ARCHIVAL_SIZE_KEY);
		String filePath = this.uploadFile(request, response, keyVal1.getKeyValue(), Long.parseLong(keyVal2.getKeyValue())*1024*1024);
		if(null != filePath){
			String info = filePath.substring(filePath.lastIndexOf("/upload")+1, filePath.length());
			map.put("archivesUrl", info);
			map.put("success", "true");
		}else{
			map.put("success", "false");
		}
		formatJSON(response, map);
		return null;
	}
	
	/**
	 * @desc 加载字典表上传附件的类型和大小
	 * @return
	 * @author dangjh
	 * @datetime 2014年11月5日,下午8:12:28
	 */
	@Action(value = "archival_file_type_size")
	public String typeAndSizeView(){
		HttpServletResponse response = ServletActionContext.getResponse();
		map = new HashMap<String, Object>();
		MedicalAdmin admin = (MedicalAdmin) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
		MedicalKeyValue keyVal1 = this.baseAllService.getDictionaryService().findKeyValByKey(admin, SystemConfig.HM_SYSTEM_TYPE, SystemConfig.UPLOAD_ARCHIVAL_KEY);
		MedicalKeyValue keyVal2 = this.baseAllService.getDictionaryService().findKeyValByKey(admin, SystemConfig.HM_SYSTEM_TYPE, SystemConfig.UPLOAD_ARCHIVAL_SIZE_KEY);
		map.put("fileType", keyVal1.getKeyValue());
		map.put("fileSize", keyVal2.getKeyValue());
		formatJSON(response, map);
		return null;
	}
	
	/**
	 * @desc 实例化选择用户列表页
	 * @return
	 * @author dangjh
	 * @datetime 2014年10月29日,下午1:57:07
	 */
	@Action(value = "init_choice_user_list", results = {@Result(location = "init_choice_user_list.jsp")})
	public String initChoiceUserList(){
		return SUCCESS;
	}
	
	/**
	 * @desc 加载选择用户数据列表
	 * @return
	 * @author dangjh
	 * @datetime 2014年10月12日,下午2:37:15
	 */
	@Action(value = "choice_user_list_view")
	public String choiceUserListView(){
		HttpServletResponse response = ServletActionContext.getResponse();
		MedicalAdmin user = (MedicalAdmin) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
		map = ServletActionContext.getContext().getParameters();
		info.setUser(user);
		pageBean = this.baseAllService.getArchivalService().findAllUsers(map, info);
		map.clear();
		map.put("total", pageBean.getTotalRow());
		map.put("data", pageBean.getResult());
		formatJSON(response, map);
		return null;
	}
	
	/**
	 * @desc 删除员工档案信息
	 * @return
	 * @author dangjh
	 * @datetime 2014年10月30日,下午12:02:16
	 */
	@Action(value = "meidcal_archival_del")
	public String archivalInfoDel(){
		HttpServletResponse response = ServletActionContext.getResponse();
		map = new HashMap<String, Object>();
		if(StringUtils.isNotBlank(info.getId())){
			this.baseAllService.getArchivalService().deleteInfo(info);
			map.put("success", true);
		}else{
			map.put("success", false);
		}
		formatJSON(response, map);
		return null;
	}
	
	/**
	 * @desc 判断附件是否存在
	 * @return
	 * @author dangjh
	 * @datetime 2014年11月4日,上午9:25:38
	 */
	@Action(value = "archival_is_exists")
	public String isExists(){
		if(StringUtils.isNotBlank(info.getId())){
			HttpServletResponse response = ServletActionContext.getResponse();
			map = new HashMap<String, Object>();
			info = this.baseAllService.getArchivalService().findById(info.getId());
			String path = ServletActionContext.getRequest().getSession().getServletContext().getRealPath(File.separator)+info.getArchivesUrl();
			boolean flag = this.baseAllService.getArchivalService().archivalIsExists(path);
			if(flag){
				map.put("success", true);
			}else{
				map.put("success", false);
			}
			formatJSON(response, map);
		}
		return null;
	}
	
	/**
	 * @desc 下载附件
	 * @return
	 * @author dangjh
	 * @datetime 2014年10月30日,下午4:02:08
	 */
	@Action(value = "medical_archival_download")
	public String downloadFile(){
		if(StringUtils.isNotBlank(info.getId())) {
			info = this.baseAllService.getArchivalService().findById(info.getId());
			String ext = info.getArchivesUrl().substring(info.getArchivesUrl().lastIndexOf("."), info.getArchivesUrl().length());
			this.download(info.getUser().getName()+"信息档案"+ext, info.getArchivesUrl());
		}
		return null;
	}
	
	@Override
	public MedicalArchivalInfo getModel() {
		return info;
	}

}
